package com.zhongke.cotrun.controller.manager;

import com.github.pagehelper.PageInfo;
import com.zhongke.cotrun.common.annotation.SysLog;
import com.zhongke.cotrun.common.util.APIResponse;
import com.zhongke.cotrun.controller.AbstractController;
import com.zhongke.cotrun.model.pojo.DictEntity;
import com.zhongke.cotrun.model.pojo.InnovationActivitiesEntity;
import com.zhongke.cotrun.model.pojo.InnovationActivitiesEntityVo;
import com.zhongke.cotrun.model.pojo.SysMessageEntity;
import com.zhongke.cotrun.model.resultpackage.CodeMsg;
import com.zhongke.cotrun.model.validator.UserValidator;
import com.zhongke.cotrun.service.DictService;
import com.zhongke.cotrun.service.InnovationActivitiesService;
import com.zhongke.cotrun.service.PayMallInfoService;
import com.zhongke.cotrun.service.SysMessageService;
import com.zhongke.cotrun.shiro.sys.shiro.ShiroUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.Map;


/**
 * 创新活动表
 *
 * @author lee
 * @email wawzj512541@gmail.com
 * @date 2019-11-15 14:59:00
 */
@Validated
@Api(tags = "创新活动管理员模块")
@RestController("innovationActivities")
@RequestMapping("/manager/innovationActivities")
public class InnovationActivitiesController extends AbstractController {

    @Autowired
    private InnovationActivitiesService innovationActivitiesService;
    @Autowired
    private PayMallInfoService payMallInfoService;
    @Autowired
    private SysMessageService sysMessageService;

    private static final String APPROVAL_PASS_TYPE = "ACTIVE_APPROVAL_PASS"; //审核通过的字典type

    private static final String APPROVAL_REJECT_TYPE = "ACTIVE_APPROVAL_REJECT"; //审核驳回的字典type

    /**
     * 新增
     *
     * @param innovationActivitiesEntity
     * @return APIResponse
     */
    @ApiOperation(value = "新增创新活动", httpMethod = "POST")
    @SysLog(value = "提交活动", type = "创新活动")
    @PostMapping("/addInnovationActivities")
    @RequiresPermissions("sys:activities:save")
    public APIResponse addInnovationActivities(@Validated({UserValidator.InSet.class}) InnovationActivitiesEntity innovationActivitiesEntity) {
        innovationActivitiesEntity.setCreateId(getSysUserId());
        if (innovationActivitiesService.addInnovationActivities(innovationActivitiesEntity)) {
            return APIResponse.returnSuccess("添加成功");
        } else {
            return APIResponse.returnFail(CodeMsg.SERVER_ERROR);
        }
    }

    /**
     * 逻辑删除
     *
     * @param id 主键id
     * @return APIResponse
     */
    @ApiOperation(value = "删除", httpMethod = "GET")
    @RequiresPermissions("sys:activities:delete")
    @SysLog(value = "删除活动", type = "创新活动")
    @GetMapping(value = {"/deleteById"}, produces = "application/json;charset=utf-8")
    public APIResponse deleteById(@NotNull(message = "参数不能为空")@ApiParam(value = "主键id", required = true) Integer id) {
        if (innovationActivitiesService.deleteByID(id)) {
            return APIResponse.returnSuccess("删除成功");
        } else {
            return APIResponse.returnFail(CodeMsg.SERVER_ERROR);
        }
    }

    /**
     * 批量删除
     *
     * @param ids 主键ids(多个id用英文逗号分隔)
     * @return APIResponse
     */
    @ApiOperation(value = "批量删除", httpMethod = "GET")
    @RequiresPermissions("sys:activities:delete")
    @SysLog(value = "批量删除活动", type = "创新活动")
    @GetMapping(value = {"/deleteByBatch"}, produces = "application/json;charset=utf-8")
    public APIResponse deleteByBatch(@NotNull(message = "参数不能为空")@ApiParam(value = "主键id,多个id用逗号分隔", required = true) String ids) {
        if (innovationActivitiesService.deleteBatch(ids)) {
            return APIResponse.returnSuccess("删除成功");
        } else {
            return APIResponse.returnFail(CodeMsg.SERVER_ERROR);
        }
    }

    /**
     * 修改
     *
     * @param innovationActivitiesEntity 实体类
     * @return APIResponse
     */
    @ApiOperation(value = "修改活动", httpMethod = "POST")
    @RequiresPermissions("sys:activities:update")
    @SysLog(value = "修改活动", type = "创新活动")
    @PostMapping("/updateById")
    public APIResponse updateById(@Validated({UserValidator.UpDate.class}) InnovationActivitiesEntity innovationActivitiesEntity) {
        if (innovationActivitiesEntity != null) {
            InnovationActivitiesEntityVo iae = innovationActivitiesService.selectByID(innovationActivitiesEntity.getId());
            if (iae.getApprovalStatus().equals(0) || iae.getApprovalStatus().equals(1))//待审核、审核通过的可以修改
            {
                if (iae.getApprovalStatus().equals(1))//审核通过的，报名截止时间、报名费、报名人数保持不变
                {
                    innovationActivitiesEntity.setRegEndTime(iae.getRegEndTime());
                    innovationActivitiesEntity.setRegFee(iae.getRegFee());
                    innovationActivitiesEntity.setRegNumber(iae.getRegNumber());
                }
                if (innovationActivitiesService.updateByID(innovationActivitiesEntity)) {
                    return APIResponse.returnSuccess("修改成功");
                } else {
                    return APIResponse.returnFail(CodeMsg.SERVER_ERROR);
                }
            } else
                return APIResponse.returnFail(CodeMsg.DATA_EMPTY);
        } else
            return APIResponse.returnFail(CodeMsg.DATA_EMPTY);
    }

    /**
     * 修改状态
     */
    @ApiOperation(value = "修改状态", httpMethod = "POST")
    @RequiresPermissions("sys:activities:update")
    @SysLog(value = "修改活动状态", type = "创新活动")
    @PostMapping("updateStatus")
    public APIResponse updateStatus(@NotNull(message = "参数不能为空")@ApiParam(value = "主键id", required = true) Integer id,
                                    @ApiParam(value = "状态", required = true)@NotNull(message = "状态不能为空") Integer status) {
        if (innovationActivitiesService.updateStatus(id, status)) {
            return APIResponse.returnSuccess("操作成功");
        } else {
            return APIResponse.returnFail(CodeMsg.SERVER_ERROR);
        }
    }

    /**
     * 根据id搜索
     *
     * @param id 主键id
     * @return APIResponse
     */
    @ApiOperation(value = "根据id查看详情", httpMethod = "GET")
    @RequiresPermissions("sys:activities:info")
    @RequestMapping(value = "/getById", method = RequestMethod.GET)
    public APIResponse getById(@NotNull(message = "参数不能为空") @ApiParam(value = "主键id", required = true)Integer id) {
        InnovationActivitiesEntityVo obj = innovationActivitiesService.selectByID(id);
        if (obj != null) {
            return APIResponse.returnSuccess(obj);
        } else {
            return APIResponse.returnFail(CodeMsg.DATA_EMPTY);
        }
    }

    /**
     * 分页展示,可根据条件筛选
     *
     * @param innovationActivitiesEntity 实体类
     * @param pageNum                    页码
     * @param pageSize                   页面尺寸
     * @return APIResponse
     */
    @ApiOperation(value = "分页展示", httpMethod = "GET")
    @RequiresPermissions("sys:activities:list")
    @RequestMapping(value = "/showByPage", method = RequestMethod.GET)
    public APIResponse showByPageForCenter(InnovationActivitiesEntityVo innovationActivitiesEntity, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo pageInfo = innovationActivitiesService.showByPageForManager(innovationActivitiesEntity, pageNum, pageSize);
        int count = innovationActivitiesService.queryUnApprovalCounts();
        Map data = new HashMap(4);
        data.put("count", count);
        data.put("data", pageInfo);
        return APIResponse.returnSuccess(data);
    }

    /**
     * 根据id显示详情
     *
     * @param id 主键id
     * @return APIResponse
     */
    @RequestMapping(value = "/showById", method = RequestMethod.GET)
    public APIResponse showById(@NotNull(message = "参数不能为空") @ApiParam(value = "主键id", required = true)Integer id) {
        InnovationActivitiesEntityVo obj = innovationActivitiesService.selectByID(id);
        if (obj != null) {
            return APIResponse.returnSuccess(obj);
        } else {
            return APIResponse.returnFail(CodeMsg.DATA_EMPTY);
        }
    }

    /**
     * 取消活动(下线活动)
     *
     * @param id 主键id
     * @return APIResponse
     */
    @ApiOperation(value = "取消活动", httpMethod = "POST")
    @RequiresPermissions("sys:activities:cancel")
    @SysLog(value = "下线活动", type = "创新活动")
    @PostMapping("/closeActivities")
    public APIResponse closeActivities(@NotNull(message = "参数不能为空") @ApiParam(value = "主键id", required = true)Integer id) {
        if (innovationActivitiesService.closeActivities(id)) {

            return APIResponse.returnSuccess();
        } else {
            return APIResponse.returnFail(CodeMsg.SERVER_ERROR);
        }
    }

    /**
     * 修改审批状态-通过
     *
     * @param id 主键id
     * @return APIResponse
     */
    @ApiOperation(value = "审批通过", httpMethod = "POST")
    @SysLog(value = "审批通过", type = "创新活动")
    @PostMapping("/updateApprovalStatusPass")
    @RequiresPermissions("sys:activities:approval")
    public APIResponse updateApprovalStatusPass(@NotNull(message = "参数不能为空") @ApiParam(value = "主键id", required = true)Integer id) {
        int approvalStatus = 1;
        if (innovationActivitiesService.updateApprovalStatusAndMall(approvalStatus, "", id)) {
            SysMessageEntity messageEntity= sysMessageService.saveSysMessage(APPROVAL_PASS_TYPE,id.longValue(),"activities");
            return APIResponse.returnSuccess();
        } else {
            return APIResponse.returnFail(CodeMsg.SERVER_ERROR);
        }
    }

    /**
     * 修改审批状态-驳回
     *
     * @param approvalOpinion 审批意见
     * @param id              主键id
     * @return APIResponse
     */
    @ApiOperation(value = "审批驳回", httpMethod = "POST")
    @SysLog(value = "审批驳回", type = "创新活动")
    @PostMapping("/updateApprovalStatusRejected")
    @RequiresPermissions("sys:activities:approval")
    public APIResponse updateApprovalStatusRejected(@NotNull(message = "参数不能为空") @ApiParam(value = "主键id", required = true)Integer id,
                                                    @NotNull(message = "驳回原因不能为空")@ApiParam(value = "驳回原因", required = true)String approvalOpinion) {
        int approvalStatus = 2;
        if (innovationActivitiesService.updateDemandsApprovalStatus(approvalStatus, approvalOpinion, id)) {
            SysMessageEntity messageEntity= sysMessageService.saveSysMessage(APPROVAL_REJECT_TYPE,id.longValue(),"activities");
            return APIResponse.returnSuccess();
        } else {
            return APIResponse.returnFail(CodeMsg.SERVER_ERROR);
        }
    }
}
